package com.mq.demo;

import com.mq.common.Consumer;
import com.mq.common.exception.MqException;
import com.mq.mqserver.core.BasicProperties;
import com.mq.mqclient.Channel;
import com.mq.mqclient.Connection;
import com.mq.mqclient.ConnectionFactory;
import com.mq.mqserver.core.ExchangeType;

import java.io.IOException;

public class DemoConsumer {
    public static void main(String[] args) throws IOException, MqException, InterruptedException {
        System.out.println("启动消费者!");
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.56.1");
        factory.setPort(9090);
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        //生产者消费者谁先启动谁先创建
        channel.exchangeDeclare("testExchange11", ExchangeType.DIRECT, true,
                false, null);
        channel.queueDeclare("testQueue11", true, false, false, null);
        channel.basicConsume("testQueue11", true, new Consumer() {
            @Override
            public void handleDelivery(String consumerTag, BasicProperties
                    properties, byte[] body) {
                System.out.println("[消费数据] 开始!");
                System.out.println("consumerTag=" + consumerTag);
                System.out.println("properties=" + properties);
                String bodyString = new String(body, 0, body.length);
                System.out.println("body=" + bodyString);
                System.out.println("[消费数据] 完毕!");
            }
        });
        //循环等待一下
        while (true) {
            Thread.sleep(500);
        }
    }

}
